package cn.com.cym.gamezao.web.grk;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.base.BaseController;
import cn.com.cym.gamezao.base.BaseService;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.dto.DiagnoseDto;
import cn.com.cym.gamezao.dto.DiagnoseExportDto;
import cn.com.cym.gamezao.dto.DiagnoseSearchDto;
import cn.com.cym.gamezao.entity.grk.Diagnose;
import cn.com.cym.gamezao.entity.single.VaccineReg;
import cn.com.cym.gamezao.service.grk.DiagnoseService;
import cn.com.cym.gamezao.util.CommonUtils;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.List;

@Api(tags = "grk-患者信息")
@RestController
@AllArgsConstructor
@RequestMapping(value = "grk/diagnose")
@AuthToken(rule = "grk.diagnose")
public class DiagnoseController extends BaseController<Diagnose> {

    final DiagnoseService thisService;
    @Override
    public BaseService<Diagnose> getService() {
        return thisService;
    }

    @ApiImplicitParam(name = "diagnose", value = "diagnose", required = true,dataType = "Diagnose")
    @ApiOperation(value = "保存")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @PostMapping("")
    @AuthToken(rule = "save",require = false)
    public ResponseEntity<?> save(@RequestBody Diagnose diagnose) {
        Diagnose save = this.thisService.save(diagnose);
        return ResponseEntity.ok(save);
    }

    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "DiagnoseSearchDto")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PostMapping("/pageSearch")
    @AuthToken(rule = "search")
    public ResponseEntity<?> pageBySearch(@RequestBody DiagnoseSearchDto searchPage){
        Page<Diagnose> byPageable = thisService.findPageBySearchDto(searchPage);
        return ResponseEntity.ok(byPageable);
    }

    /**
     * 导出
     * @params startDate s
     * @return ？
     * @throws FileNotFoundException
     */
    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "DiagnoseSearchDto")
    @ApiOperation(value = "导出")
    @ApiOperationSupport(order = 6, author = "mr.chen")
    // @AuthToken(rule = "exportData")
    @PostMapping(value = "/exportData",produces= {MediaType.APPLICATION_OCTET_STREAM_VALUE})
    public void exportData(@RequestBody DiagnoseSearchDto searchPage, HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        List<DiagnoseExportDto> list = this.thisService.getAllBySearchPage(searchPage);
        // 通过工具类创建writer
        String aLong = CommonUtils.customIp(request) + LocalDateTime.now().getLong(ChronoField.MICRO_OF_SECOND) + ".xlsx";

        ExcelWriter writer = ExcelUtil.getWriter(CommonUtils.filePath(aLong));
        //自定义标题别名
        writer.addHeaderAlias("diagnoseTime", "就诊时间").setColumnWidth(0, 30);
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("regNum", "登记号")
                .setColumnWidth(2, 12);
        writer.addHeaderAlias("sampleNo", "样本编号")
                .addHeaderAlias("position", "冰箱存储位置")
                .setColumnWidth(4,60)
                .addHeaderAlias("diagnoseId", "诊断")
                .addHeaderAlias("medicine", "治疗方案")
                .setColumnWidth(6,60);
        headers.setContentDisposition(ContentDisposition.attachment()
                .filename(aLong)
                .build());
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        writer.setOnlyAlias(true);
        writer.write(list, true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + aLong);
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }
    
    @ApiImplicitParam(name = "id", value = "id", required = true,dataType = "Long")
    @ApiOperation(value = "获取数据")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/dto/{id}")
    @AuthToken(rule = "search",require = false)
    public ResponseEntity<?> getDtoById(@PathVariable Long id){
        return ResponseEntity.ok(this.thisService.getDtoById(id));
    }
}
